#include "sys_ext.h"
#include "app_timer.h"
#include "systask.h"
#include "fsl_gpt.h"
#include "dataproc.h"
#include "pincfg.h"

/*****************************************************************************//*!
* @brief   	app timer init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/ 
void app_timer_init(void)
{
    gpt_config_t config;
    
    set_u8(&config,0,sizeof(config));
    
    config.clockSource = kGPT_ClockSource_Periph;
    config.divider = CLOCK_GetFreq(kCLOCK_PerClk)/1000000;
    
    GPT_Init(GPT1, &config);
    
    GPT1->OCR[0] = 999;
    GPT1->OCR[1] = GPT1->OCR[0] + 1000;
    GPT1->OCR[2] = GPT1->OCR[0] + 1000;
    
    //compare 1 is reset mode
    GPT1->CR &= ~GPT_CR_FRR_MASK;
    
    //enable isr 
    GPT1->IR = GPT_IR_OF1IE_MASK;
    
    nvic_irq_set(GPT1_IRQn, 5, 1);
    
    GPT1->CR |= GPT_CR_EN_MASK;
}

/*****************************************************************************//*!
* @brief   	app timer isr.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/ 
void mb03_TimeoutHandle(void);
void GPT1_IRQHandler(void)
{
    //wtite 1 to clear
    GPT1->SR = 0x3F;
    
    sysst.ticks++;
    mb03_TimeoutHandle();
    
    SDK_ISR_EXIT_BARRIER;
}

